package t4invoke;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import t4bean.FruitBean;
import t4bean.PurchaseRecordBean;
import t4dao.FruitDao;
import t4dao.PurchaseRecordDao;

public class PurchaseAction implements Action {

	@Override
	public void purchase(Scanner sc) {
		System.out.println("输入水果名字");
		String fruitName = sc.nextLine();
		System.out.println("输入进价");
		Integer cost = Integer.parseInt(sc.nextLine());
		System.out.println("输入售价");
		Integer price = Integer.parseInt(sc.nextLine());
		System.out.println("输入数量");
		Integer number = Integer.parseInt(sc.nextLine());
		FruitBean fruit = new FruitBean();
		fruit.setName(fruitName);
		FruitDao fd = new FruitDao();
		List<FruitBean> list = new ArrayList<>();
		try {
			list = fd.select(fruit);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		fruit.setPrice(price);
		fruit.setCost(cost);
		if (list.size() != 0) {
			fruit.setNumber(number + list.get(0).getNumber());
			fd.update(fruit);
		} else {
			fruit.setNumber(number);
			fd.insert(fruit);
		}
		PurchaseRecordBean prb = new PurchaseRecordBean();
		LocalDateTime t = LocalDateTime.now();
		prb.setPurchaseTime(t);
		prb.setFruitName(fruitName);
		prb.setNumber(number);
		prb.setCost(cost);
		PurchaseRecordDao prd = new PurchaseRecordDao();
		prd.insert(prb);
		System.out.println("进货成功");

	}

	@Override
	public void sell(Scanner sc) {

	}

	@Override
	public void moneyGet(Scanner sc) {

	}

	@Override
	public void showAllSellRecord() {

	}

	@Override
	public void three() {

	}

	@Override
	public void showFruit() {

	}

}
